import { Controller } from 'egg';
import validateInput from '../decorator/inputValidate'
import checkPermission from '../decorator/checkPermission'
// 验证输入的格式是否正确
const workCreateRules = {
  title: 'string'
}

export interface IndexCondition {
  pageIndex?: number;
  pageSize?: number;
  select?: string | string[];
  populate?: { path?: string; select?: string } | string;
  customSort?: { [key: string]: any }// {num:12,sun:'sdf'}
  find?: Record<string, any> // {[key:string]:any} //此处为8-6课程
}

export default class UserController extends Controller {
  @validateInput(workCreateRules, 'workValidateFail') // 利用装饰器简化验证信息代码 去掉重复逻辑
  async createWork() {
    const { ctx, service, app } = this;
    const workData = await service.work.createEmptyWork(ctx.request.body)
    ctx.helper.success({ ctx, res: workData });
  }
  async myList() {
    const { ctx } = this;
    const userId = ctx.state.user._id
    const { pageIndex, pageSize, isTemplate, title } = ctx.query;
    const findConditon = {
      user: userId,
      ...(title && { title: { $regex: title, $options: 'i' } }), // 假如有title 就给添加上对应值 mongodb $regex 正则表达式 代表title部分匹配 i 代表大小写不管
      ...(isTemplate && { isTemplate: !!parseInt(isTemplate) }) // !!parseInt(isTemplate) 先转int类型再转布尔类型 boolean

    }
    const lsitCondition: IndexCondition = {
      select: 'id author copiedCount coverImg desc title user isHot createdAt',
      populate: { path: 'user', select: 'username nickName picture' },
      find: findConditon,
      ...(pageIndex && { pageIndex: parseInt(pageIndex) }),
      ...(pageSize && { pageSize: parseInt(pageSize) })
    }
    const res = await ctx.service.work.getList(lsitCondition);
    ctx.helper.success({ ctx, res })
  }
  async templateList() {
    const { ctx } = this
    const { pageIndex, pageSize } = ctx.query
    const listCondition: IndexCondition = {
      select: 'id author copiedCount coverImg desc title user isHot createdAt',
      populate: { path: 'user', select: 'username nickName picture' },
      find: { isPublic: true, isTemplate: true },
      ...(pageIndex && { pageIndex: parseInt(pageIndex) }),
      ...(pageSize && { pageSize: parseInt(pageSize) })
    }
    const res = await ctx.service.work.getList(listCondition)
    ctx.helper.success({ ctx, res })
  }
  @checkPermission('Work', 'workCheckPermission')
  /**
   * 更新内容
   */
  async update() {
    const { ctx } = this
    const { id } = ctx.params
    const payload = ctx.request.body
    const res = await this.ctx.model.Work.findOneAndUpdate({ id }, payload, { new: true }).lean()
    ctx.helper.success({ ctx, res })
  }
  @checkPermission('Work', 'workCheckPermission')
  async deleteWork() {
    const { ctx } = this;
    const { id } = ctx.params
    const res = await ctx.model.Work.findOneAndDelete({ id }).select('_id id title').lean()
    ctx.helper.success({ ctx, res })
  }

  @checkPermission('Work', 'workCheckPermission')
  async publish(isTemplate: boolean) {
    const { ctx } = this
    const url = await this.service.work.publish(ctx.params.id, isTemplate)
    ctx.helper.success({ ctx, res: { url } })
  }
  async publishWork() {
    await this.publish(false)
  }
  async publishTemplate() {
    await this.publish(true)
  }
}
